/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Principal;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import javax.swing.JLabel;

/**
 *
 * @author EliteBook
 */
public final class stock extends javax.swing.JDialog {

    /**
     * Creates new form stock
     */
    JLabel[][] labels;
    PreparedStatement ps;
    ResultSet rs;
    static int codigo;
    int nsuc = 0;

    public stock(java.awt.Frame parent, boolean modal, int idpr) {
        super(parent, modal);
        initComponents();
        codigo = idpr;
        cargarDatos();
    }

    void cargarDatos() {
        try {
            int i, j;
            ps=local_connection.getconnect().prepareStatement("select nombre from productos where id_producto =?");
            ps.setInt(1,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                nombre.setText(rs.getString(1));
            id.setText(String.valueOf(codigo));
            ps = (PreparedStatement) local_connection.getconnect().prepareStatement("select count(*) from stock where id_producto = ?"+((VentanaPrincipal.mostrarAlmacen)?"":" and sucursal>0"));
            ps.setInt(1, codigo);
            rs = ps.executeQuery();
            if (rs.next()) {
                if (rs.getInt(1) != 0) {
                    nsuc = rs.getInt(1);
                }
            }if(nsuc>0){
            //if(!VentanaPrincipal.mostrarAlmacen)nsuc--;
             float[] cantidad = new float[nsuc];
             String[] ubicacion = new String[nsuc];
                        String[] sucursal = new String[nsuc];
                        ps=(PreparedStatement)local_connection.getconnect().prepareStatement("select nombre, ubicacion, cantidad from stock join sucursales on sucursales.id= stock.sucursal where id_producto = ?"+((VentanaPrincipal.mostrarAlmacen)?"":" and sucursales.id>0"));
                        ps.setInt(1,codigo);
                        rs=ps.executeQuery();               
                        int n=0;//esta variable checa si hay registros
                        while(rs.next()){
                            sucursal[n]=rs.getString("nombre");
                            ubicacion[n]=rs.getString("ubicacion");
                            cantidad[n]=rs.getFloat("cantidad");
                            n++;
                        }
                        if(n==0){
                            sucursal[n]="";
                            ubicacion[n]="";
                            cantidad[n]=0.0f;
                        }
                        panelSucursales.setLayout(new GridBagLayout());
                       GridBagConstraints c = new GridBagConstraints();
                       c.anchor = GridBagConstraints.NORTHWEST;
                       labels = new JLabel[nsuc][3];
                       c.weightx = 0.5;
                       c.ipadx=100;
                        c.insets = new Insets(3,3,3,3);
                       for(i=0;i<nsuc;i++){
                           for(j=0;j<3;j++){
                               labels[i][j]= new JLabel();
                               c.gridx = j;
                               c.gridy = i;
                               /*textFieldConstraints.gridx = i;
                               textFieldConstraints.fill = GridBagConstraints.HORIZONTAL;
                               textFieldConstraints.weightx = 0.5;
                               textFieldConstraints.insets = new Insets(10, 10, 10, 10);
                               textFieldConstraints.gridy = j;*/
                               labels[i][j].setPreferredSize(new Dimension(20, 20));
                               panelSucursales.add(labels[i][j],c);  
                               if(j==0)labels[i][j].setText(sucursal[i]);
                               if(j==1)labels[i][j].setText(String.valueOf(ubicacion[i]));
                               if(j==2)labels[i][j].setText(String.valueOf(cantidad[i]));}
                           
                       }
                            panelSucursales.updateUI();
                     //  panelPrecios.revalidate();
                       this.pack();}
        } catch (SQLException ex) {
            VentanaPrincipal.logger.log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelSucursales = new javax.swing.JPanel();
        id = new javax.swing.JLabel();
        nombre = new javax.swing.JLabel();
        Cantidad = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Stock de producto");
        setResizable(false);

        panelSucursales.setLayout(new java.awt.GridBagLayout());

        id.setText("00000");

        nombre.setText("Nombre");

        Cantidad.setText("Cantidad");

        jLabel1.setText("Sucursal");

        jLabel2.setText("Ubicacion");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(panelSucursales, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(id)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(nombre)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(105, 105, 105)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 112, Short.MAX_VALUE)
                        .addComponent(Cantidad)
                        .addGap(79, 79, 79))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(id)
                    .addComponent(nombre))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(Cantidad))
                .addGap(1, 1, 1)
                .addComponent(panelSucursales, javax.swing.GroupLayout.DEFAULT_SIZE, 12, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Cantidad;
    private javax.swing.JLabel id;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel nombre;
    private javax.swing.JPanel panelSucursales;
    // End of variables declaration//GEN-END:variables
}
